<?php
namespace app\home\controller;
use \think\Validate;
use \think\captcha\Captcha;
use think\Session;

class Own extends Common
{
    public function own()
    {
    	$user_id = session("user_id");
        $this->assign("typeaa","own");
        $this->assign("user_id",$user_id);
    	return view('own');
    }

    public function address(){
    	$user_id = session("user_id");
    	$address_list = db('address')->order("is_default desc")->select();
        $this->assign("address_list",$address_list);
        $this->assign("typeaa","own");
        $this->assign("user_id",$user_id);
    	return view('address');
    }

    public function add_address(){
        $this->assign("typeaa","own");
    	return view('add_address');
    }

    public function addAddressAction(){
    	$user_id = session("user_id");
    	$name 	 = $_POST['name'];
    	$tell 	 = $_POST['tell'];
    	$content = $_POST['content'];
    	$tag 	 = $_POST['tag'];

    	if(!array_key_exists('is_default',$_POST)){
    		$is_default = 0;
    	}else{
    		$is_default = 1;
    	}
    	$data = [
    		"user_id" => $user_id,
    		"name" 	  => $name,
    		"tell" 	  => $tell,
    		"content" => $content,
    		"tag" 	  => $tag,
    		"is_default"=> $is_default,
    		"addtime" => time()
    	];
    	$ret = db('address')->insert($data);
    	if($ret){
    		$this->success("地址添加成功","/home/own/address");
    	}else{
    		$this->error("地址添加失败","/home/own/address");
    	}
    }

    public function modify_address(){
    	$address_id = $_GET['address_id'];
    	$address_info = db('address')->where("id = {$address_id}")->find();
        $this->assign("typeaa","own");
        $this->assign("address_info",$address_info);
        $this->assign("address_id",$address_id);
    	return view('modify_address');
    }

    public function modifyAddressAction(){
    	$user_id = session("user_id");
    	$address_id = $_POST['address_id'];
    	$name 	 = $_POST['name'];
    	$tell 	 = $_POST['tell'];
    	$content = $_POST['content'];
    	$tag 	 = $_POST['tag'];
    	
    	if(!array_key_exists('is_default',$_POST)){
    		$is_default = 0;
    	}else{
    		$is_default = 1;
    	}
    	$data = [
    		"user_id" => $user_id,
    		"name" 	  => $name,
    		"tell" 	  => $tell,
    		"content" => $content,
    		"tag" 	  => $tag,
    		"is_default"=> $is_default
    	];
    	$ret = db('address')->where("id={$address_id}")->update($data);
    	$result = db('address')->where("id!={$address_id} && is_default=1")->find();
    	if($is_default==1 && $result!=NULL){
    		$ret2 = db('address')->where("id='{$result["id"]}'")->update(array("is_default" => 0));
	    	if($ret2){
	    		$this->success("地址修改成功","/home/own/address");
	    	}else{
	    		$this->error("地址修改失败","/home/own/address");
	    	}
    	}else{
	    	if($ret){
	    		$this->success("地址修改成功","/home/own/address");
	    	}else{
	    		$this->error("地址修改失败","/home/own/address");
	    	}
    	}
	}
	
	public function pickaddress(){
		$user_id = session("user_id");
		$order_id = $_GET['order_id'];
    	$address_list = db('address')->order("is_default desc")->select();
        $this->assign("address_list",$address_list);
        $this->assign("typeaa","own");
        $this->assign("user_id",$user_id);
        $this->assign("order_id",$order_id);
    	return view('pickaddress');
	}

	public function pickChange(){
		$address_id = $_POST['address_id'];
		$order_id 	= $_POST['order_id'];
		$ret = db('order')->where("id = {$order_id}")->update(array("address_id"=> $address_id));
		return ['status' => 1,'order_id'=>$order_id];
	}
}
